import random

from flask import Flask, render_template, redirect, url_for, session, g
from flask_bootstrap import Bootstrap4
from flask_migrate import Migrate
from werkzeug.security import generate_password_hash

import config
from ext import db, moment,mail,csrf
from bluelog import art, aut
from modle import EmailCaptchaModel, UserModel, AnswerModel, QuestionModel, CategoryModel
from faker import Faker

fake = Faker(local="zh_CN")

app = Flask(__name__)
bootstrap = Bootstrap4(app)
app.config.from_object(config)
moment.init_app(app)
db.init_app(app)
mail.init_app(app)
csrf.init_app(app)
migrate = Migrate(app, db)

app.register_blueprint(aut, url_prefix='/auth/')
app.register_blueprint(art)

# 每发一个请求就执行一遍
@app.before_request
def before_request():
    user_id = session.get('user_id')
    if user_id:
        try:
            user = UserModel.query.get(user_id)
            # setattr(g,"user",user)
            g.user = user
        except:
            g.user = None

# 每发一个请求就执行一遍 ，但放回的必须是字典
@app.context_processor
def context_processor():
    if hasattr(g, "user"):
        return {"user": g.user}
    else:
        return {}
        


# @app.route('/add/init')
# def text_user():
#     db.drop_all()
#     db.create_all()
#     name = fake.name()
#     admin = UserModel()
#     admin.username = name
#     admin.password = generate_password_hash('123123')
#     admin.email = fake.email()
#     admin.is_root = 1
#     db.session.add(admin)
#     db.session.commit()
#     # return redirect(url_for('text_category'))
#     return redirect('/')
#
#
# @app.route('/add/articles')
# def text():
#     admin = UserModel.query.filter(UserModel.id == 1).first()
#     category_list = CategoryModel.query.all()
#     for i in range(10):
#         art1 = QuestionModel()
#         art1.title = f'测试{i}'
#         art1.author = admin
#         art1.explain = fake.sentence()
#         art1.category = random.choice(category_list)
#         db.session.add(art1)
#         num1 = random.randint(1, 10)
#         for i in range(num1):
#             com1 = AnswerModel()
#             com1.author = admin
#             com1.content = fake.sentence()
#             com1.question = art1
#             db.session.add(com1)
#             num2 = random.randint(1,5)
#             for i in range(num2):
#                 rep1 = AnswerModel()
#                 rep1.author = admin
#                 rep1.to_user = admin
#                 rep1.content = fake.sentence()
#                 rep1.replied = com1
#                 rep1.question = com1.question
#                 db.session.add(rep1)
#     db.session.commit()
#     return redirect(url_for("article.home"))
#
#
# @app.route('/add/category')
# def text_category():
#     cat_list = ['python', 'javascrip', 'c语言', 'c++', '玩具代码']
#     for i in range(0, 5):
#         cat1 = CategoryModel()
#         cat1.name = cat_list[i]
#         db.session.add(cat1)
#         db.session.commit()
#     return redirect(url_for("text"))


if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port='8787')
